/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package post2;
import java.io.*;
import java.lang.*;
import java.math.BigDecimal;

/**
 *
 * @author danielcalvo
 */
public class ProductReader {
    BufferedReader scanner;
    //Scanner scanner;
    
    public ProductReader(String productFile){
        try{
            //Read File
            scanner = new BufferedReader(new FileReader(productFile));   
            //scanner = new Scanner(new File(productFile));         
        }
        catch (FileNotFoundException e){
            System.out.println("Products file could not be found/read!");
        }
        
    }
    
    /*Checks if there are more products to be read*/ 
    boolean hasMoreProducts(){
        try{
            return scanner.ready();
        }
        catch (IOException e){
            System.out.println("No more Products to be read!");
            return false;
        }
    }
   
    /*Grabs product from file and sets Product Specs*/
    ProductSpec getNextProduct(){
        ProductSpec product = new ProductSpec();
        String record = "";//Single Product record from file
        
        try{
            record = scanner.readLine();
            //record = scanner.nextLine();
        }
        catch (IOException e){System.out.println("Product unable to be read!");}
        
        //Set Product Specifications as Products are read
        product.setUPC(record.substring(0, 4));
        product.setDescription(record.substring(9,29));
        product.setPrice(new BigDecimal(record.substring(35)));
            
        return product;
    }
    
    
//    public static void main(String args[]){
//        //Test
//        ProductReader in = new ProductReader("products.txt");
//        ProductSpec test;
//        
//        while (in.hasMoreProducts()){
//            test = in.getNextProduct();
//            System.out.print(test.getDescription() + " ");
//            System.out.print(test.getUPC() + " ");
//            System.out.println(test.getPrice()); 
//        }
//    }
    
    
}
